//
//  HomeArtWorkCell.m
//  jingdongfang
//
//  Created by LJL on 2018/5/8.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeArtWorkCell.h"
#import "IntroViewController.h"

@interface HomeArtWorkCell ()
{
    UIView *backView;
    
    UIImageView *imgView;
    UIImageView *tras;
    UILabel *name;
    UIView *header;
    UIImageView *icon;
    UILabel *user;
    UILabel *year;
    UILabel *cla;
    UILabel *num;
    UIButton *pushBtn;
    UILabel *gifV;
}

@end

@implementation HomeArtWorkCell

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor mainBackgroundColor];
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor c3Color];
    [self.contentView addSubview:backView];
    
    //图片
    imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    [backView addSubview:imgView];
    
    tras = [[UIImageView alloc] initWithFrame:CGRectZero];
    UIImage *image = [UIImage imageNamed:@"B-16-2"];
    [image stretchableImageWithLeftCapWidth:4 topCapHeight:2];
    tras.image = image;
    [imgView addSubview:tras];
    
    name = [[UILabel alloc] initWithFrame:CGRectZero];
    name.textColor = [UIColor c3Color];
    name.font = [UIFont boldSystemFontOfSize:18];
    [tras addSubview:name];
    
    header = [[UIView alloc] initWithFrame:CGRectZero];
    header.backgroundColor = [UIColor c3Color];
    [self.contentView addSubview:header];
    
    icon = [[UIImageView alloc] initWithFrame:CGRectZero];
    icon.layer.masksToBounds = YES;
    icon.layer.cornerRadius = 12.5;
    icon.contentMode = UIViewContentModeScaleAspectFill;
    [header addSubview:icon];
    
    user = [[UILabel alloc] initWithFrame:CGRectZero];
    user.font = [UIFont systemFontOfSize:14];
    user.textColor = [UIColor c4Color];
    [header addSubview:user];
    
    pushBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [pushBtn setTitle:@"推送" forState:UIControlStateNormal];
    //    [pushBtn setTitle:NSLocalizedString(@"Push", nil) forState:UIControlStateNormal];
    [pushBtn setTitle:NSLocalizedString(@"Push", nil) forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [pushBtn setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [pushBtn addTarget:self action:@selector(pushBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:pushBtn];
    
    num = [[UILabel alloc] initWithFrame:CGRectZero];
    num.font = [UIFont systemFontOfSize:12];
    num.textColor = [UIColor c5Color];
    num.textAlignment = NSTextAlignmentRight;
    [header addSubview:num];
    
    icon.userInteractionEnabled = YES;
    user.userInteractionEnabled = YES;
    __weak typeof(self) weakSelf = self;
    [icon setTapActionWithBlock:^{
        [weakSelf clickBtn];
    }];
    [user setTapActionWithBlock:^{
        [weakSelf clickBtn];
    }];
    
    gifV = [[UILabel alloc] init];
    gifV.text = @"GIF";
    gifV.backgroundColor = [UIColor c4Color];
    gifV.alpha = 0.5;
    gifV.textColor = [UIColor c3Color];
    gifV.font = [UIFont systemFontOfSize:15];
    gifV.textAlignment = NSTextAlignmentCenter;
    gifV.layer.cornerRadius = 15;
    gifV.layer.masksToBounds = YES;
    gifV.hidden = YES;
    [imgView addSubview:gifV];
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-75);
    }];
    
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(65);
    }];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.centerX.mas_equalTo(backView.mas_centerX);
        make.bottom.mas_equalTo(0);
    }];
    
    [tras mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-15);
    }];
    
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.height.mas_equalTo(25);
        make.centerY.mas_equalTo(header.centerY);
    }];
    
    [pushBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(header.centerY);
    }];
    
    [num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(pushBtn.mas_left).offset(-10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(header.centerY);
    }];
    
    [user mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(icon.mas_right).mas_offset(10);
        make.right.mas_equalTo(num.mas_left).mas_offset(-10);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [gifV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
}

- (void)clickBtn
{
    IntroViewController *introVC = [[IntroViewController alloc] init];
    introVC.u_id = _recommendDetail.uploaderId;
    [UIViewController ig_pushViewController:introVC animated:YES];
}

- (void)setRecommendDetail:(IGArtWorkModel *)recommendDetail
{
    if (_recommendDetail && _recommendDetail == recommendDetail) {
        return;
    }
    _recommendDetail = recommendDetail;
    
    CGFloat width = KSCREENWIDTH - 20;
    if (![recommendDetail.plates isEqualToString:@"1"]) {
        width = (self.height-75-10) * 9/16;
    }
    [imgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
    
    [imgView sd_setImageWithURL:[NSURL URLWithString:recommendDetail.image] placeholderImage:[UIImage imageFromColor:[UIColor mainRandomColor]]];
    [icon sd_setImageWithURL:[NSURL URLWithString:recommendDetail.uploaderAvatar] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    name.text = recommendDetail.title;
    user.text = recommendDetail.uploader;
    if (recommendDetail.pushNums) {
        //        num.text = [NSString stringWithFormat:@"%@人", recommendDetail.pushNums];
        
        //        if ([self isChinese]) {
        //            num.text = [NSString stringWithFormat:@"%@人", recommendDetail.pushNums];
        //        } else
        num.text = [NSString stringWithFormat:@"%@", recommendDetail.pushNums];
    } else {
        //        num.text = @"0人";
        //        if ([self isChinese]) {
        //            num.text = @"0人";
        //        } else
        num.text = @"0";
    }
    gifV.hidden = [recommendDetail.isGif isEqualToString:@"1"] ? NO : YES;
}

-(BOOL)isChinese{
    NSString *clanguage = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
    if ([clanguage containsString:@"zh-Hans"]) {
        return YES;
    } else return NO;
}

- (void)pushBtnClick:(UIButton *)sender
{
    if (self.pushBlock)
    {
        self.pushBlock();
    }
}



@end
